import 'package:enterprise_v6_app/app/components/custom_modele/custom_card.dart';
import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/data/entities/application/major_hazard/hazard_obligation_history_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/major_hazard/obligation/history/violation_controller.dart';

// #FFF5F5F6
const _bg = Color.fromARGB(255, 245, 245, 246);
// #FC9900
const _yellow = Color.fromARGB(255, 252, 153, 0);
// #5C6DFF
const _blue = Color.fromARGB(255, 92, 109, 255);
// #FFA0A7B5
const _fontDisable = Color.fromARGB(255, 160, 167, 181);
// #FFA0A7B5
const _bgDisable = Color.fromARGB(55, 160, 167, 181);

class HazardObligationsViolationPage extends StatelessWidget {
  HazardObligationsViolationPage({super.key});

  final controller = Get.put(HazardObligationsViolationController());

  final String missionId = Get.parameters['missionId'] ?? '';
  final String type = Get.parameters['type'] ?? '';

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: controller,
      builder: (_) {
        return Scaffold(
          appBar: AppBar(
            centerTitle: true,
            title: Text(
              '历史逾期记录',
              style: TextStyle(fontSize: 16.w),
            ),
          ),
          body: _buildBody(),
        );
      },
    );
  }

  Widget _buildBody() {
    return Container(
      color: _bg,
      child: Column(
        children: [
          AppGaps.vGap6,
          _buildTip(),
          AppGaps.vGap16,
          _buildSelect(),
          Expanded(
            child: CustomEasyRefreshList<HazardObligationHistoryEntity>(
              key: ValueKey(controller.year.value),
              dataLoad: (i, s, {String? search}) => controller.dataLoad(
                i,
                s,
                missionId,
                type,
              ),
              itemBuilder: _buildCard,
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildTip() {
    return Container(
      color: Colors.white,
      padding: EdgeInsets.symmetric(vertical: 6.w, horizontal: 16.w),
      child: Text(
        '核查技术负责人、操作负责人是否按规定时间、规定内容履行职责。',
        style: TextStyle(color: _yellow, fontSize: 13.w),
      ),
    );
  }

  Widget _buildSelect() {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 16.w),
      child: Row(
        children: [
          _buildSwitchBtn(HazardViolationYearType.current),
          AppGaps.hGap10,
          _buildSwitchBtn(HazardViolationYearType.last),
          AppGaps.hGap10,
          _buildSwitchBtn(HazardViolationYearType.beforeLast),
        ],
      ),
    );
  }

  Widget _buildSwitchBtn(HazardViolationYearType type) {
    return InkWell(
      onTap: () {
        controller.year.value = type;
        controller.update();
      },
      child: Container(
        padding: EdgeInsets.symmetric(vertical: 2.w, horizontal: 17.w),
        decoration: BoxDecoration(
          color: controller.year.value == type ? _blue : _bgDisable,
          borderRadius: BorderRadius.circular(26.w),
        ),
        child: Text(
          type.name,
          style: TextStyle(
            color: controller.year.value == type ? Colors.white : _fontDisable,
            fontSize: 14.w,
          ),
        ),
      ),
    );
  }

  Widget _buildCard(HazardObligationHistoryEntity data, int idx) {
    return Padding(
      padding: EdgeInsets.all(16.w),
      child: CustomCard(
        padding: EdgeInsets.symmetric(horizontal: 11.w, vertical: 14.w),
        titleWidget: Padding(
          padding: EdgeInsets.only(bottom: 14.w),
          child: Row(
            children: [
              SvgPicture.asset(AppImages.icon_title),
              AppGaps.hGap10,
              Text(
                data.riskEventName ?? '未知',
                softWrap: true,
                maxLines: 2,
                style: TextStyle(fontSize: 12.w, fontWeight: FontWeight.w500),
              ),
            ],
          ),
        ),
        divider: CustomCard.builtinDivider,
        content: Column(
          children: [AppGaps.vGap16, _buildRecord()],
        ),
      ),
    );
  }

  Widget _buildRecord() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        // #FF495061
        Text(
          '2021-01-01',
          style: TextStyle(
            color: const Color.fromARGB(255, 73, 80, 97),
            fontSize: 13.w,
          ),
        ),
        // #FFFD6B6B
        Text(
          '逾期',
          style: TextStyle(
            color: const Color.fromARGB(255, 253, 107, 107),
            fontSize: 12.w,
          ),
        )
      ],
    );
  }
}
